package bill.java.lab.client.netSuite;

public class NetSuiteTest {

	/**
	 * QUESTION 1
	 * 
	 * Given a sorted array of integers, write a method to remove the
	 * duplicates. Do not use any classes from the java.util package, or the
	 * equivalent library for your language.
	 * 
	 * Example: [ 1, 2, 3, 3, 3, 4, 4, 10, 13, 15, 15, 17 ] -> [ 1, 2, 3, 4, 10,
	 * 13, 15, 17 ]
	 * 
	 * Solution template:
	 * 
	 * @param values
	 * @return
	 */

	int[] removeDuplicates(int[] values) {
		if (values == null) {
			throw new IllegalArgumentException(
					"The parameter values cannot be null");
		}

		if (values.length == 0) {
			return new int[0];
		}

		int[] temp = new int[values.length];
		int i = 1;
		int j = 1;
		int current = values[0];
		temp[0] = values[0];
		while (i < values.length) {
			if (current == values[i]) {
				i++;
			} else {
				temp[j++] = values[i];
				current = values[i++];
			}

		}

		int[] result = new int[j];
		for (int k = 0, l = 0; k < j; k++) {
			result[k] = temp[l++];

		}
		return result;
	}

	/**
	 *  A) Write a method that prints out the all the values in sorted order,
	 * given the  root Node. Assume that 'root' is not null. 
	 * Solution
	 * template:
	 * @param root
	 */
	void printOrdered(Node root) {
		if (root == null) {
			return;
		} else {
			printOrdered(root.left);
			System.out.print(root.value + ",");
			printOrdered(root.right);
		}
	}

	/**
	 * B) Write a method that inserts a new value, given the root Node of the
	 * tree. Assume that 'root' is not null.
	 * 
	 * Solution template:
	 * @param root
	 * @param newValue
	 */
	void insertValue(Node root, int newValue) {
		Node y = null;
		Node x = root;

		while (x != null) {
			y = x;
			if (newValue < x.value)
				x = x.left;
			else
				x = x.right;
		}

		Node newNode = new Node(newValue);
		y.left = newNode;

		if (y == null)
			root = newNode; // the tree had been empty
		else {
			if (newValue <= y.value)
				y.left = newNode;
			else
				y.right = newNode;
		}
	}

	private static class Node {
		int value;
		Node left;
		Node right;

		Node(int value) {
			this.value = value;
		}
	}

	public static void main(String[] args) {
		int[] values = new int[] { 1, 2, 3, 3, 3, 4, 4, 10, 13, 15, 15, 17 };

		int[] values1 = new int[] { 1 };
		int[] result = new NetSuiteTest().removeDuplicates(values1);
		printArray(result);
	}

	private static void printArray(int[] arr) {
		for (int i = 0, j = 0; i < arr.length; i++) {
			System.out.print(arr[i] + ", ");
		}
	}
}
